package config

import (
	"fmt"
	"os"
	"strings"

	"gopkg.in/ini.v1"
)

type ClientConfig struct {
	State     bool
	Type      int
	Addr      string
	Handler   string
	Reconnect bool
}

type ServerConfig struct {
	TcpServer    string
	Heartbeat    int
	DataInterval int
	ReadTimeout  int
	WriteTimeout int
	Luminosity   int
	CheckInter   int
	LeadTime     int
	LastTime     int
	ClientHB     int
	ClientSleep  int
	Monitor      int
	AirPort      string
	RunWayNum    int
	LogSaveSize  int
	Clients      []ClientConfig // 新增客户端配置
}

var Config ServerConfig

func InitConfig(configFile string) {
	cfg := LoadConfig(configFile)
	sectionList := "adsb_client,awos_client,weather_client,flightplan_client,videorecog_client"
	// 加载客户端配置
	var clientCfgs []ClientConfig
	if cfg != nil {
		for _, section := range cfg.Sections() {
			if strings.Contains(sectionList, section.Name()) {
				clientCfg := ClientConfig{
					State:     section.Key("state").MustBool(false),
					Type:      section.Key("type").MustInt(1),
					Addr:      section.Key("addr").MustString(""),
					Handler:   section.Key("handler").MustString(""),
					Reconnect: section.Key("reconnect").MustBool(true),
				}

				if clientCfg.Type > 0 {
					clientCfgs = append(clientCfgs, clientCfg)
				}
			}
		}

		Config = ServerConfig{
			TcpServer:    GetIniFileToStr(cfg, "server", "tcp_server", "0.0.0.0:19090"),
			Heartbeat:    GetIniFileToInt(cfg, "server", "heartbeat", 5),
			DataInterval: GetIniFileToInt(cfg, "server", "data_interval", 5),
			ReadTimeout:  GetIniFileToInt(cfg, "server", "read_timeout", 100),
			WriteTimeout: GetIniFileToInt(cfg, "server", "write_timeout", 100),
			Luminosity:   GetIniFileToInt(cfg, "server", "luminosity", 0),
			CheckInter:   GetIniFileToInt(cfg, "server", "check_inter", 0),
			LeadTime:     GetIniFileToInt(cfg, "server", "lead_time", 900),
			LastTime:     GetIniFileToInt(cfg, "server", "last_time", 3600),
			ClientHB:     GetIniFileToInt(cfg, "client", "client_hb", 5),
			ClientSleep:  GetIniFileToInt(cfg, "client", "client_sleep", 5),
			Monitor:      GetIniFileToInt(cfg, "client", "monitor", 5),
			AirPort:      GetIniFileToStr(cfg, "airport", "air_port", ""),
			RunWayNum:    GetIniFileToInt(cfg, "airport", "runway_num", 1),
			LogSaveSize:  GetIniFileToInt(cfg, "log", "log_save_size", 100),
			Clients:      clientCfgs,
		}
	}
}

func LoadConfig(configFile string) *ini.File {
	if _, err := os.Stat(configFile); os.IsNotExist(err) {
		fmt.Printf("config file not found: %s", configFile)
		return nil
	}

	cfg, err := ini.Load(configFile)
	if err != nil {
		fmt.Printf("failed to read config file: %v", configFile)
		return nil
	}
	return cfg
}

func GetIniFileToInt(cfg *ini.File, section string, key string, defint int) int {
	server := cfg.Section(section)
	return server.Key(key).MustInt(defint)
}

func GetIniFileToStr(cfg *ini.File, section string, key string, defstr string) string {
	server := cfg.Section(section)
	// fmt.Println(server.Key(key).MustString(defstr))
	return server.Key(key).MustString(defstr)
}

//func GetIniFileToBool(cfg *ini.File, section string, key string, defbool bool) bool {
//	server := cfg.Section(section)
//	return server.Key(key).MustBool(defbool)
//}
